"""
全国疫情可视化地图开发
"""
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts

# 读取文件中的疫情数据
file_path = "/Users/dongjian/Documents/学习/python-learn-资料/资料/第1-12章资料/资料/可视化案例数据/地图数据/疫情.txt"
f = open(file_path, "r", encoding="UTF-8")
data = f.read()
# print(type(data))
# print(data)

# areaTree 0
data_dict = json.loads(data)
# 获取省份数据
province_data_list = data_dict["areaTree"][0]["children"]

# 组装每个省份和确诊人数的元组，并将各省数据分装到列表中
data_list = []  # 绘图还有的数据
tuple_ = ("上海", "重庆", "天津", "北京")
dict_ = {"广西": "广西壮族自治区", "西藏": "西藏自治区", "新疆": "新疆维吾尔自治区", "内蒙古": "内蒙古自治区",
         "宁夏": "宁夏回族自治区", "香港": "香港特别行政区", "澳门": "澳门特别行政区", "南海": "南海诸岛"}
for province_data in province_data_list:
    province_name = province_data["name"]  # 省份名称
    if tuple_.count(province_name) > 0:
        province_name = province_name + "市"
    elif dict_.get(province_name):
        province_name = dict_[province_name]
    else:
        province_name = province_name + "省"
    province_confirm = province_data["total"]["confirm"]  # 确诊人数
    data_list.append((province_name, province_confirm))
data_list.append(("南海诸岛", 50))
print(data_list)

map = Map()
map.add("全国疫情地图", data_list, "china")

# 设置全局选项，定制分段的视觉映射
map.set_global_opts(
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min": 1, "max": 99, "label": "1-99人", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "label": "100-999人", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "label": "1000-4999人", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "label": "1000-9999人", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "label": "10000-99999人", "color": "#CC3333"},
            {"min": 100000, "label": "100000+", "color": "#990033"}
        ]
    )
)
# 绘图
map.render("全国疫情地图.html")
